#!/bin/bash
if [ "$EUID" -ne 0 ] ; then
    sudo /bin/bash "$0" "$@"
    exit $?
fi

if [ ! "$BASH_VERSION" ] ; then
    exec /bin/bash "$0" "$@"
    exit $?
fi

set -e

STORAGE_SSD="/data/ssd"
STORAGE_HDD="/data/hdd"
INSTALL_DIR="/home/xf/pacs"


PKG_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
APPS_PACKAGE=$PKG_DIR/pacs.tgz

check_dependences() {
    if ! docker info > /dev/null 2>&1; then
        echo "ERR: docker 没有安装或启动"
        exit 1
    fi

    if ! docker-compose > /dev/null 2>&1; then
        echo "ERR: docker-compose 没有安装或加载"
        exit 1
    fi

    if [[ ! -d "$STORAGE_SSD" ]] ; then
        mkdir -p $STORAGE_SSD
    fi

    if [[ ! -d "$STORAGE_HDD" ]] ; then
        mkdir -p $STORAGE_HDD
    fi


    if [[ ! -f "$APPS_PACKAGE" ]] ; then
        echo "ERR: pacs包不存在"
        exit 1
    fi

}

check_dependences

echo "Extracting apps"
mkdir -p $INSTALL_DIR
tar -xf $APPS_PACKAGE -C $INSTALL_DIR

IP=`hostname -I | cut -d' ' -f1`

sed -i "s/#{本机IP}/${IP}/g" $INSTALL_DIR/settings.env

echo "安装完成"
